(function() {
  var $shortcutListWrapper = $('#shortcut-list-wrapper');
  var $shortcutList = $('#shortcut-list');
  var $createShortcutLink = $('#create-shortcut-link');
  var $importShortcutLink = $('#import-shortcut-link');
  
  var sl = window.ShortcutList = {
    shortcuts: {},
    setup: function() {
      $createShortcutLink.on('click', function() {
        ShortcutSetting.createShortcut();
        sl.hide();
      });

      $importShortcutLink.on('click', Manager.importShortcut);
      sl.constructShortcutList();
    },

    constructShortcutList: function() {
      var shortcuts = ShortcutsStorage.getShortcuts();

      // Add shortcut into shortcut list.
      for (var shortcutId in shortcuts) {
        var shortcut = shortcuts[shortcutId];
        var shortcutData = Fs.getShortcutData(shortcut.name, shortcut.path);
        if ($.type(shortcutData) == 'object') {
          shortcutData.id = ShortcutsStorage.getShortcutId(shortcut.name,
            shortcut.path);
          sl.addShortcut(shortcutData);
        } else {
          // Read shortcut data from file system failed
          ShortcutsStorage.removeShortcut(shortcutId);
        }
      }

      if (!Manager.createdShortcut()) {
        sl.hide();
        Welcome.show();
      }
    },

    show: function() {
      $shortcutListWrapper.show();
      $createShortcutLink.show();
      $importShortcutLink.show();
    },

    hide: function() {
      $shortcutListWrapper.hide();
      $createShortcutLink.hide();
      $importShortcutLink.hide();
    },

    /**
     * Add shortcut item to shortcut list.
     * @param {Object} shortcut
     *   required keys: id, name, path, iconId, icon, iconFile, iconIndex,
     *   themeName, userDataFolder, switches
     */
    addShortcut: function(shortcut) {
      var shortcutId = shortcut.id;
      var li = document.createElement('li');
      var template = $('#shortcut-item-template').html();

      var i18nTitle = i18n['click_to_launch'] + ' ' + shortcut.name;
      $(li).attr('id', shortcutId).attr('title', i18nTitle).html(template);
      $(li).on('click', function(e) {
        var _shortcut = sl.shortcuts[shortcutId];
        var target = e.target;
        var $target = $(target);
        if (target.tagName == 'IMG' && ($target.hasClass('edit-icon') ||
            $target.hasClass('delete-icon'))) {
          // Handle edit and delete icons.
          e.stopPropagation();
          if ($target.hasClass('edit-icon')) {
            ShortcutSetting.applyShortcutData(_shortcut);
            ShortcutSetting.show();
            ShortcutList.hide();
          } else if ($target.hasClass('delete-icon')) {
            sl.deleteShortcut(shortcutId);
          }
        } else {
          // Launch shortcut
          Manager.startup(shortcutId);
        }
      });

      sl.updateShortcut(shortcut, li);
      $shortcutList.append(li);
      sl.shortcuts[shortcutId] = shortcut;
    },

    /**
     * Update shortcut item name and icon.
     * @param {Object} shortcut
     *   required keys: id, name, path, iconId, icon, iconFile, iconIndex,
     *   themeName, userDataFolder, switches
     * @param {HTMLLIElement} li
     */
    updateShortcut: function(shortcut, li) {
      $('img.shortcut-icon', li).attr('src', shortcut.icon);
      var $shortcutName = $('span.shortcut-name', li);
      $shortcutName.text(shortcut.name).attr('title', shortcut.name);

      sl.shortcuts[shortcut.id] = shortcut;
    },

    /**
     * Delete shortcut from shortcut list and fs by shortcut id.
     * @param {String} shortcutId
     * @param {Boolean} noConfirm Delete shortcut without user's confirmation.
     */
    deleteShortcut: function(shortcutId, noConfirm) {
      if (noConfirm) {
        delShortcut(true);
      } else {
        Tip.confirmToDeleteShortcut(function(result) {
          var alsoDeleteShortcutFromFs = false;
          if (result == 'also_delete_shortcut')
            alsoDeleteShortcutFromFs = true;
          if (result != 'cancel')
            delShortcut(alsoDeleteShortcutFromFs);
        });
      }

      function delShortcut(alsoDeleteShortcutFromFs) {
        var shortcut = ShortcutsStorage.getShortcut(shortcutId);
        if (shortcut) {
          if (alsoDeleteShortcutFromFs) {
            // Delete shortcut from local storage and file system.
            var deleteResult = Fs.removeShortcut(shortcut.name, shortcut.path);

            // Make sure that succeed to delete shortcut from file system, then
            // remove it from local storage.
            if (deleteResult) {
              deleteShortcutFromLocalstorage();
            } else {
              // Delete shortcut from file system failed.
              Tip.alert('shortcut_delete_failed');
            }
          } else {
            deleteShortcutFromLocalstorage();
          }
        } else {
          // Retrieve shortcut data from local storage failed.
          Tip.alert('shortcut_retrieve_failed');
        }
      }

      // Just delete shortcut from local storage.
      function deleteShortcutFromLocalstorage() {
        $shortcutList.removeChild($id(shortcutId));
        ShortcutsStorage.removeShortcut(shortcutId);
        sl.checkEmptyShortcutList();
      }
      
    },

    checkEmptyShortcutList: function() {
      if (!Manager.createdShortcut()) {
        Welcome.show();
        sl.hide();
      }
    }
  };
})();